package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>implements IWmChannelService {

    @Override
    public ResponseResult listByName(ChannelDto channelDto) {

        IPage<WmChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.like(!StringUtils.isEmpty(channelDto.getName()),WmChannel::getName,channelDto.getName());
      page=page(page,queryWrapper);
        return new PageResponseResult<Object> (channelDto.getPage(),channelDto.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    public ResponseResult saveChannel(WmChannel channel) {
        //如果传来必须得参数为空
        if(channel==null||StringUtils.isEmpty(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName,channel.getName());
        WmChannel wmChannel = this.getOne(queryWrapper);
        if(wmChannel!=null)
        {//已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        channel.setCreatedTime(new Date());
        this.save(channel);
        return ResponseResult.okResult();
    }
}

